package com.jw.qme.utils;

/**
 * ClassName: com.jw.qme.utils
 * Datetime: 2025/4/19 下午10:01
 * Description:
 *
 * @author 汪佳泉
 * @version 1.0
 * @since 1.0
 */
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.ByteBuffer;
import java.util.Base64;

public class AESUtil {

    // AES 加密方法
    public static String encrypt(String key, String data) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.getDecoder().decode(key), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); // 使用 ECB 模式和 PKCS5 填充
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        byte[] encryptedBytes = cipher.doFinal(data.getBytes("UTF-8"));
        return Base64.getEncoder().encodeToString(encryptedBytes); // 返回 Base64 编码的加密数据
    }

    // AES 解密方法
    public static String decrypt(String key, String encryptedData) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.getDecoder().decode(key), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); // 使用 ECB 模式和 PKCS5 填充
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
        return new String(decryptedBytes, "UTF-8"); // 返回解密后的原始数据
    }

    // AES 加密方法（ByteBuffer 数据）
    public static ByteBuffer encryptByteBuffer(String key, ByteBuffer data) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.getDecoder().decode(key), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

        // 将 ByteBuffer 转换为字节数组
        byte[] inputBytes = new byte[data.remaining()];
        data.get(inputBytes); // 读取数据到字节数组

        // 加密数据
        byte[] encryptedBytes = cipher.doFinal(inputBytes);

        // 返回加密后的 ByteBuffer
        return ByteBuffer.wrap(encryptedBytes);
    }

    // AES 解密方法（ByteBuffer 数据）
    public static ByteBuffer decryptByteBuffer(String key, ByteBuffer encryptedData) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.getDecoder().decode(key), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);

        // 将 ByteBuffer 转换为字节数组
        byte[] inputBytes = new byte[encryptedData.remaining()];
        encryptedData.get(inputBytes); // 读取数据到字节数组

        // 解密数据
        byte[] decryptedBytes = cipher.doFinal(inputBytes);

        // 返回解密后的 ByteBuffer
        return ByteBuffer.wrap(decryptedBytes);
    }

    // 测试方法
    public static void main(String[] args) {
        try {
            // 示例密钥（128 位，Base64 编码）
            String symmetricKey = "SyYDOwcFUURDPliG7MAkng==";

            // 待加密的数据
            String originalData = "Hello, World!";

            // 加密
            String encryptedData = encrypt(symmetricKey, originalData);
            System.out.println("加密后的数据: " + encryptedData);

            // 解密
            String decryptedData = decrypt(symmetricKey, encryptedData);
            System.out.println("解密后的数据: " + decryptedData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}